package com.ruoyi.employment.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.student.domain.XzkStudent;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.employment.domain.XzkEmployment;
import com.ruoyi.employment.service.IXzkEmploymentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 就业学员表Controller
 * 
 * @author ruoyi
 * @date 2021-04-02
 */
@Controller
@RequestMapping("/employment/employment")
public class XzkEmploymentController extends BaseController
{
    private String prefix = "employment/employment";

    @Autowired
    private IXzkEmploymentService xzkEmploymentService;

    @RequiresPermissions("employment:employment:view")
    @GetMapping()
    public String employment()
    {
        return prefix + "/employmentstudent";
    }


    /**
     * 前台模板
     */
    private void getColorType( XzkStudent student){
        if (student.getSuperviseclasslasttime()!=null){
            Date date=new Date();
            int days = (int) ((date.getTime() - student.getSuperviseclasslasttime().getTime()) / (1000*3600*24));
            if (days<=5){
                student.setColorType(1);
            }else if(days<=8){
                student.setColorType(2);
            }else {
                student.setColorType(3);
            }
        }else {
            student.setColorType(1);
        }
    }

    /**
     * 查询就业学员表列表
     */

    @RequiresPermissions("employment:employment:list")
    @PostMapping("/list3")
    @ResponseBody
    public TableDataInfo list3(XzkStudent xzkEmployment, HttpSession session)
    {
        String userName = (String)session.getAttribute("userName");
        startPage();
        List<XzkStudent> list = xzkEmploymentService.selectXzkEmploymentList3(xzkEmployment,userName);
        for (XzkStudent student : list) {
            getColorType(student);
        }
        return getDataTable(list);
    }

    /**
     * 导出就业学员表列表
     */
    @RequiresPermissions("employment:employment:export")
    @Log(title = "就业学员表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(XzkEmployment xzkEmployment)
    {
        List<XzkEmployment> list = xzkEmploymentService.selectXzkEmploymentList(xzkEmployment);
        ExcelUtil<XzkEmployment> util = new ExcelUtil<XzkEmployment>(XzkEmployment.class);
        return util.exportExcel(list, "employment");
    }

    /**
     * 新增就业学员表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存就业学员表
     */
    @RequiresPermissions("employment:employment:add")
    @Log(title = "就业学员表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(XzkEmployment xzkEmployment)
    {
        return toAjax(xzkEmploymentService.insertXzkEmployment(xzkEmployment));
    }

    /**
     * 修改就业学员表
     */
    @GetMapping("/edit/{employmentid}")
    public String edit(@PathVariable("employmentid") Long employmentid, ModelMap mmap)
    {
        XzkEmployment xzkEmployment = xzkEmploymentService.selectXzkEmploymentById(employmentid);
        mmap.put("xzkEmployment", xzkEmployment);
        return prefix + "/edit";
    }

    /**
     * 修改保存就业学员表
     */
    @RequiresPermissions("employment:employment:edit")
    @Log(title = "就业学员表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(XzkEmployment xzkEmployment)
    {
        return toAjax(xzkEmploymentService.updateXzkEmployment(xzkEmployment));
    }

    /**
     * 删除就业学员表
     */
    @RequiresPermissions("employment:employment:remove")
    @Log(title = "就业学员表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(xzkEmploymentService.deleteXzkEmploymentByIds(ids));
    }
}
